<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改文章</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            margin: 0;
            padding: 20px;
        }

        .container {
            max-width: 600px;
            margin: auto;
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }

        h1 {
            text-align: center;
            color: #333;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }

        input[type="text"],
        textarea {
            width: 100%;
            padding: 10px;
            margin-bottom: 20px;
            border: 1px solid #ccc;
            border-radius: 4px;
            font-size: 16px;
        }

        textarea {
            resize: vertical;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            border: none;
            border-radius: 4px;
            color: white;
            font-size: 16px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #218838;
        }

        .message {
            text-align: center;
            margin: 20px 0;
        }

        .message p {
            padding: 10px;
            border-radius: 4px;
        }

        .message.success {
            background-color: #d4edda;
            color: #155724;
        }

        .message.error {
            background-color: #f8d7da;
            color: #721c24;
        }

        a {
            display: block;
            text-align: center;
            margin-top: 20px;
            color: #007bff;
            text-decoration: none;
        }

        a:hover {
            text-decoration: underline;
        }

        /* 新增样式：用于隐藏原始的文件输入框 */
        input[type="file"] {
            display: none;
        }

        /* 新增样式：用于显示上传附件的提示信息 */
        .attachment-upload-status {
            text-align: center;
            margin: 10px 0;
            color: green;
            display: none;
        }

        /* 新增样式：用于显示上传附件失败的错误信息 */
        .attachment-upload-error {
            text-align: center;
            margin: 10px 0;
            color: red;
            display: none;
        }
    </style>
</head>
<body>

<div class="container">
    <h1>修改文章</h1>
    <form th:action="@{/personalhomepage/revise}" method="post" enctype="multipart/form-data">
        <input type="hidden" name="articleId" th:value="${article.article_id}">

        <label for="articleTitle">文章标题：</label>
        <input type="text" id="articleTitle" name="articleTitle" th:value="${article.article_title}" required>

        <label for="articleText">文章内容：</label>
        <textarea id="articleText" name="articleText" rows="10" required th:text="${article.article_text}"></textarea>

        <!-- 新增：上传新附件的文件输入框，先隐藏 -->
        <input type="file" id="newArticleAttachment" name="articleAttachment" style="display:none;">
        <!-- 新增：触发文件输入框的按钮 -->
        <button type="button" id="chooseNewAttachmentButton">选择新附件</button>

        <button type="submit" id="tijiao">提交修改</button>

        <!-- 新增：用于显示上传附件提示信息的元素 -->
        <p class="attachment-upload-status" th:text="${message}" th:if="${message!= null}"></p>
        <p class="attachment-upload-error" th:text="${error}" th:if="${error!= null}"></p>
    </form>

    <div class="message" th:if="${message}">
        <p class="success" th:text="${message}"></p>
    </div>
    <div class="message" th:if="${error}">
        <p class="error" th:text="${error}"></p>
        <a href="/personalhomepage">返回个人主页</a>
    </div>
    <a href="/personalhomepage">返回用户个人页面</a>
    <script>

        document.addEventListener('DOMContentLoaded', function () {
            // 为选择新附件按钮添加点击事件
            const chooseNewAttachmentButton = document.getElementById('chooseNewAttachmentButton');
            const newArticleAttachment = document.getElementById('newArticleAttachment');

            chooseNewAttachmentButton.addEventListener('click', function () {
                newArticleAttachment.click();
            });

            // 监听表单提交事件
            const form = document.querySelector('form');
            form.addEventListener('submit', function (e) {
                e.preventDefault(); // 阻止表单默认提交行为

                // 弹出询问确定窗口
                if (confirm("你确定要提交修改吗？")) {
                    const formData = new FormData(form);

                    // 这里可以添加代码来发送AJAX请求，将表单数据（包括附件）发送到后端进行处理
                    // 示例使用fetch API，你也可以使用其他如jQuery的$.ajax等方法

                    fetch(form.action, {
                        method: form.method,
                        body: formData
                    })
                        .then(response => response.json())
                        .then(data => {
                            if (data.message) {
                                // 显示上传附件成功的提示信息
                                document.querySelector('.attachment-upload-status').textContent = data.message;
                                document.querySelector('.attachment-upload-status').style.display = 'block';
                                document.querySelector('.attachment-upload-error').style.display = 'none';
                            } else if (data.error) {
                                // 显示上传附件失败的提示信息
                                document.querySelector('.attachment-upload-error').textContent = data.error;
                                document.querySelector('.attachment-upload-error').style.display = 'block';
                                document.querySelector('.attachment-upload-status').style.display = 'none';
                            }

                            // 根据后端返回结果决定是否重新加载页面或进行其他操作
                            if (data.success) {
                                location.reload();
                            }
                        })
                        .catch(error => {
                            // 处理请求错误，显示通用的上传失败提示信息
                            document.querySelector('.attachment-upload-error').textContent = '上传附件或修改文章时出现错误，请重试。';
                            document.querySelector('.attachment-upload-error').style.display = 'block';
                            document.querySelector('.attachment-upload-status').style.display = 'none';
                        });
                }
            });
        });
    </script>
</body>
</html>